package storage

import (
	"context"

	"mall/service/wpgql/api/internal/graph/model"
)

func (m *MemoryStorage) PutUser(ctx context.Context, user *model.User) error {
	m.users[user.ID] = user

	return nil
}

func (m *MemoryStorage) GetUsers(ctx context.Context, ids []string) ([]*model.User, error) {
	output := make([]*model.User, 0, len(ids))
	for _, id := range ids {
		if user, ok := m.users[id]; ok {
			output = append(output, user)
		}
	}

	return output, nil
}

func (m *MemoryStorage) GetAllUsers(ctx context.Context) ([]*model.User, error) {
	output := make([]*model.User, 0, len(m.users))
	for _, user := range m.users {
		output = append(output, user)
	}

	return output, nil
}

func (m *MemoryStorage) RetrieveUserByCredentials(
	ctx context.Context,
	userLogin string,
	password string,
) (*model.User, error) {
	if userLogin == "admin" && password == "123456" {
		nicename := "admin"
		username := "administrator"
		return &model.User{
			ID:         "1",
			DatabaseID: 1,
			Nicename:   &nicename,
			Username:   &username,
		}, nil
	}

	return nil, nil
}
